package com.csun.cmny.provider.controller.life;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcRoomAlertSilentTime;
import com.csun.cmny.provider.model.dto.life.LifeTransducerConfigDto;
import com.csun.cmny.provider.model.dto.life.RoomAlertStatusDto;
import com.csun.cmny.provider.model.vo.life.LifeTransducerConfigVO;
import com.csun.cmny.provider.model.vo.life.RoomAlertTimeVO;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcLifeTransducerDefaultConfigService;
import com.csun.cmny.provider.service.DmcLifeTransducerRoomConfigService;
import com.csun.cmny.provider.service.DmcRoomAlertSilentTimeService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping
public class DmcLifeTransducerConfigController extends BaseController {

    @Resource
    private DmcLifeTransducerDefaultConfigService dmcLifeTransducerDefaultConfigService;
    @Resource
    private DmcLifeTransducerRoomConfigService dmcLifeTransducerRoomConfigService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcRoomAlertSilentTimeService dmcRoomAlertSilentTimeService;

    /**
     * 根据房间id查询房间传感器设置
     * @date 2019/12/25 13:18
     * @param roomId
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @GetMapping(value = "/lifeTransducerConfig")
    public Wrapper getConfigByRoomId(@RequestParam(name = "roomId") Long roomId) {
        if(roomId == null || roomId.equals(0L)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("room.id.null", null));
        }
        LifeTransducerConfigVO lifeTransducerConfig = dmcLifeTransducerRoomConfigService.getConfigByRoomId(roomId,getLoginAuthDto());

//        return WrapMapper.ok(lifeTransducerConfig);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                lifeTransducerConfig);
    }

    /**
     * 根据传感器配置id保存房间传感器配置
     * @date 2019/12/25 13:42
     * @param dto
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PutMapping(value = "/lifeTransducerConfig")
    public Wrapper setConfigByRoomId(LifeTransducerConfigDto dto) {
        if(dto.getRoomId() == null || dto.getRoomId().equals(0L)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("room.id.null", null));
        }
        int ret = dmcLifeTransducerRoomConfigService.updateById(dto,getLoginAuthDto());
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }

    /**
     * 查询机构传感器默认配置
     * @date 2019/12/25 13:19
     * @param groupId
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @GetMapping(value = "/defaultLifeTransducerConfig")
    public Wrapper getGroupDefault(@RequestParam(name = "groupId") Long groupId) {
        if(groupId == null || groupId.equals(0L)){
            groupId = getLoginAuthDto().getGroupId();
        }
        LifeTransducerConfigVO lifeTransducerConfig = dmcLifeTransducerDefaultConfigService.getDefaultConfigByGroupId(groupId);
//        return WrapMapper.ok(lifeTransducerConfig);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                lifeTransducerConfig);
    }

    /**
     * 修改机构传感器默认配置
     * @date 2019/12/25 13:19
     * @param dto
     * @return com.csun.cmny.provider.controller.life.DmcLifeTransducerConfigController
     */
    @PutMapping(value = "/defaultLifeTransducerConfig")
    public Wrapper editGroupDefault(LifeTransducerConfigDto dto) {
        if(dto.getGroupId() == null || dto.getGroupId().equals(0L)){
            dto.setGroupId(getLoginAuthDto().getGroupId());
        }
        Integer ret = dmcLifeTransducerDefaultConfigService.editGroupDefaultConfig(dto);
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }

    /**
     * 获取当前房屋报警状态
     * @date 2020/1/17 13:46
     * @param roomId
     * @return com.csun.cmny.provider.service.DmcRoomAlertSilentTimeService
     */
    @GetMapping(value = "/roomAlertStatus/{roomId}")
    public Wrapper getRoomAlertStatus(@PathVariable Long roomId){

        if(roomId == null || roomId.equals(0L)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("room.id.null", null));
        }
        RoomAlertTimeVO vo = dmcRoomAlertSilentTimeService.getRoomAlertStatus(roomId);
//        return WrapMapper.ok(vo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                vo);
    }

    /**
     * 设置房间报警状态
     * @date 2020/1/17 14:03
     * @param dto, loginAuthDto
     * @return com.csun.cmny.provider.service.DmcRoomAlertSilentTimeService
     */
    @PostMapping(value = "/roomAlertStatus")
    public Wrapper setgetRoomAlertStatus(@RequestBody RoomAlertStatusDto dto){
        int ret = dmcRoomAlertSilentTimeService.setgetRoomAlertStatus(dto,getLoginAuthDto());
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }

    /**
     * 设置房间报警时间
     * @date 2020/1/17 14:03
     * @param dto, loginAuthDto
     * @return com.csun.cmny.provider.service.DmcRoomAlertSilentTimeService
     */
    @PostMapping(value = "/roomSilentTime")
    public Wrapper setRoomSilentTime(@RequestBody RoomAlertStatusDto dto){
        int ret = dmcRoomAlertSilentTimeService.setRoomSilentTime(dto,getLoginAuthDto());
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }

    /**
     * 解除房间报警屏蔽
     * @date 2020/1/17 14:34
     * @param roomId, loginAuthDto
     * @return com.csun.cmny.provider.service.DmcRoomAlertSilentTimeService
     */
    @DeleteMapping(value = "/roomAlertStatus/{roomId}")
    public Wrapper deleteRoomAlertStatus(@PathVariable Long roomId){
        int ret = dmcRoomAlertSilentTimeService.deleteRoomAlertStatus(roomId,getLoginAuthDto());
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                ret);
    }
}
